import cv2
import numpy as np
import PoseMoudle as pm
import time

cap = cv2.VideoCapture(0) # "C:\\Users\\86191\\Videos\\fwc.mp4"
cap.set(3,1280)
cap.set(4,720)
detector = pm.PoseMoudle()
count = 0
dir = 0
Ptime = 0
while True:
    ret,frame = cap.read()
    pose_frame = detector.findPose(frame,draw=False)
    lm_list = detector.findPosition(pose_frame,draw=False)
    # print(lm_list)
    if len(lm_list) > 0:
        # left angle
        # angle = detector.findAngle(frame,11,13,15)
        # right angle
        angle = detector.findAngle(frame, 12, 14, 16)
        per = np.interp(angle,(210,310),(0,100))
        # print(angle,per)
        bar = np.interp(angle,(210,310),(650,100))
        if per == 100:
            if dir == 0:
                dir = 1
                count += 0.5
        if per == 0:
            if dir == 1:
                dir = 0
                count += 0.5

        cv2.rectangle(frame, (1100, 100), (1175, 650), (0, 255, 0), 3)
        cv2.rectangle(frame, (1100, int(bar)), (1175, 650), (0, 0, 255), cv2.FILLED)
        cv2.putText(frame, f'{int(per)}%', (1100, 75), cv2.FONT_HERSHEY_PLAIN, 4, (0, 0, 255), 4)

        cv2.rectangle(frame,(0,450),(250,720),(0,255,0),cv2.FILLED)
        cv2.putText(frame,str(int(count)),(45,670),cv2.FONT_HERSHEY_PLAIN,15,(0,0,255),15)


    Ctime = time.time()
    fps = 1 / (Ctime - Ptime)
    Ptime = Ctime
    cv2.putText(frame, 'FPS:' + str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3, (0, 255, 0), 3)
    cv2.imshow('frame',frame)
    cv2.waitKey(1)